﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web;
using StudentWeb.DAL;
using StudentWeb.Model;
using StudentWeb.Common;
namespace StudentWeb.BLL
{
    public class UserBO
    {
        #region 用户信息 登录
        public IResult Login(string username, string password)
        {
            var list = new UserDataDAO().GetList();
            var userList = list.Where(item => item.UserName == username).ToList();
        if (userList.Count < 1)//如该用户记录不存在
            {
                return new Result(ResultCode.Fail, "用户名不存在");
            }
            if (userList[0].PassWord != password)//判断密码
            {
                return new Result(ResultCode.Fail, "密码错误");
            }
            return new Result(ResultCode.Success, "登录成功");
        }
        #endregion

        #region 用户信息 保存
        public bool SvaeUserInfo(dynamic data)
        {
            int UserDataID = data.UserDataID ?? 0;
            var result = 0;
            if (UserDataID > 0)
            {
                var record = new UserDataDAO().GetInfo(UserDataID);
                record.UserName = data.UserName;
                record.PassWord = data.PassWord;
                result = new UserDataDAO().Update(record);
            }
            else if (UserDataID == 0)
            {
                var user = new UserData()
                {
                    UserName = data.UserName,
                    PassWord = data.PassWord,
                };
                result = new UserDataDAO().Insert(user);
            }
            return result > 0;
        }
        #endregion

        #region 用户信息 全部记录
        public IResult GetUserAllList()
        {
            var list = new UserDataDAO().GetList();
            return new DataResult(list);
        }
        #endregion

        #region 用户信息 添加
        public bool InsertUserInfo(dynamic data)
        {
            var user = new UserData()
            {
                UserName = data.UserName,
                PassWord = data.PassWord,
            };
            var result = new UserDataDAO().Insert(user);
            return result > 0;
        }
        #endregion


        #region 用户信息 单条记录
        public IResult GetUserInfo(int UserDataID)
        {
            var record = new UserDataDAO().GetInfo(UserDataID);
            return new DataResult(record);
        }
        #endregion


        #region 用户信息 删除
        public IResult DeleteUserInfo(int UserDataID)
        {
            var res = new UserDataDAO().Delete(UserDataID);
            if (res > 0)
            {
                return new Result(ResultCode.Success, "删除成功");
            }
            else
            {
                return new Result(ResultCode.Fail, "删除失败");
            }
        }
        #endregion

        #region 用户信息 分页列表
        /// <summary>
        /// 用户信息 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="wc"></param>
        /// <returns></returns>
        public IResult GetUserList(int pageIndex, int pageSize, string wc = null)
        {
            var list = new UserDataDAO().GetList(pageIndex, pageSize, wc);
            return new DataResult(list);
        }
        #endregion
    }
}